package com.sh.data.engine.application.service.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sh.data.engine.application.model.request.api.DataApiMyApplyRequest;
import com.sh.data.engine.common.enumDefinition.DataApiApproveStatusEnum;
import com.sh.data.engine.common.util.ServletUtils;
import com.sh.data.engine.domain.api.model.domain.DataApiMyApplyDomain;
import com.sh.data.engine.domain.api.model.param.DataApiMyApplyParam;
import com.sh.data.engine.domain.api.service.DataApiAuthService;
import com.sh.data.engine.domain.api.service.DataApiMyApplyService;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.base.model.UserContext;
import com.sh.data.engine.domain.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: mengzheng.mhc
 * @date: 2024/8/16 9:45
 */
@Slf4j
@Service
public class DataApiMyApplyApplicationService {

    @Autowired
    private DataApiMyApplyService dataApiMyApplyService;

    @Autowired
    private DataApiAuthService dataApiAuthService;

    public PageResult<DataApiMyApplyDomain> getMyApplyApiPage(DataApiMyApplyRequest dataApiApproveRequest) {
        String userId = UserContext.getUserId();
        Long projectId = ServletUtils.getProjectId();
        DataApiMyApplyParam dataApiMyApplyParam =
            ConvertUtil.copyProperties(dataApiApproveRequest, DataApiMyApplyParam.class);
        dataApiMyApplyParam.setUserId(userId);
        dataApiMyApplyParam.setProjectId(projectId);
        IPage<DataApiMyApplyDomain> myApplyApiDomainPage =
            dataApiMyApplyService.getMyApplyApiDomainPage(dataApiMyApplyParam);

        return PageResult.copyPropertiesAndConvert(myApplyApiDomainPage, DataApiMyApplyDomain.class);
    }

    public boolean applyApiById(Long id) {
        return dataApiAuthService.applyApiByAuthId(id);
    }

    public boolean cancelApiApproveById(Long id) {
        return dataApiAuthService.cancelUserApiAuthById(id, DataApiApproveStatusEnum.WITHDRAW.getApproveStatus());
    }

    public boolean deleteApproveById(Long id) {
        return dataApiAuthService.deleteApiApproveById(id);
    }
}
